package com.linewell.daoImpl;

import com.linewell.dao.IUserDao;
import com.linewell.pojo.UserInfo;
import com.linewell.util.UUIDUtil;
import com.linewell.util.query.QueryBuilder;
import com.linewell.util.query.QueryEntity;
import com.linewell.util.query.QueryModel;
import com.linewell.util.query.QueryOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * @Title: springBoot+mongodb+thymeleaf
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/4/28
 */
@Repository
public class UserDao implements IUserDao {

    @Autowired
    MongoTemplate mongoTemplate;

    public UserInfo checkPunsterName(String punsterName) {
        Query query = new Query(Criteria.where("punsterName").is(punsterName));
        return mongoTemplate.findOne(query, UserInfo.class);
    }

    public void saveUserInfo(UserInfo userInfo) {
        userInfo.setUserId(UUIDUtil.getUUID());
        mongoTemplate.insert(userInfo);
    }

    public UserInfo checkNamePwd(Map<String, QueryEntity> map) {
        QueryBuilder builder = new QueryBuilder(map,QueryModel.AND);
        Criteria ca = builder.queryCriteria();
        Query query = new Query(ca);
        return mongoTemplate.findOne(query,UserInfo.class);
    }
}
